package com.zn.config;

import com.zn.common.constants.Constants;
import com.zn.interceptor.AuthInterceptor;
import com.zn.interceptor.LogInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyWebConfig implements WebMvcConfigurer {
    /**
     * SameSite=Lax  sessionId 不一致 跨域请求处理换成这个了 ,就可以了 .  可忽略 , 用于前后端分离.
     *
     * @return
     */
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        // 设置允许跨域请求的域名
        config.addAllowedOrigin("*");
        // 是否允许证书 不再默认开启
        config.setAllowCredentials(true);
        // 设置允许的方法
        config.addAllowedMethod("*");
        // 允许任何头
        config.addAllowedHeader("*");
        config.addExposedHeader("token");
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", config);
        return new CorsFilter(configSource);
    }

    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {
            //拦截 用于判断权限
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(new LogInterceptor())
                        .addPathPatterns("/**").
                        excludePathPatterns("/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg",
                                "/**/*.jpeg", "/**/*.gif", "/**/fonts/*", "/**/*.svg","/**/*.ico"
                                ,"/**/*.woff2","/**/*.map");
                registry.addInterceptor(new AuthInterceptor())
                        .addPathPatterns("/user/**", "/admin/**");
            }

            //前端获取到本地资源映射
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler(Constants.REPORT_IMG_VIRTUAL_UTL + "**").addResourceLocations("file:/" + Constants.REPORT_IMG_LOCAL_URL);
            }
            //
//            /**
//             * 跨域请求处理
//             * @param registry
//             */
//            @Override
//            public void addCorsMappings(CorsRegistry registry) {
//                registry.addMapping("/**")
//                        .allowedOrigins("*")
//                        .allowedHeaders("*")
//                        .allowCredentials(true)
//                        .allowedMethods("*")
//                        .exposedHeaders("token")
//                        .maxAge(3600);
//            }
        };
    }
}
